package org.example.joinblog.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 配置分页拦截器
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 优化 join 查询
        paginationInnerInterceptor.setOptimizeJoin(true);
        // 设置数据库类型为 MySQL
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        // 当请求页数超出范围时，返回最后一页数据
        paginationInnerInterceptor.setOverflow(true);
        // 添加分页拦截器到 MyBatis Plus 拦截器中
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        // 配置乐观锁拦截器
        OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor = new OptimisticLockerInnerInterceptor();
        // 添加乐观锁拦截器到 MyBatis Plus 拦截器中
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor);
        return interceptor;
    }

}
